#include "ioc.h"
// 回调函数，处理接收到的消息
void on_message(struct mosquitto *mqtt, void *userdata, const struct mosquitto_message *message)
{
    printf("Received message '%.*s' on topic '%s'\n", message->payloadlen, (char *)message->payload, message->topic);

    cJSON *root = cJSON_ParseWithLength(message->payload, message->payloadlen);

    // 获取第一个键值
    cJSON *first_item = root->child;
    // 获取第二个键值
    cJSON *second_item = first_item->next;

    // 根据设备类型处理
    if (cJSON_GetObjectItem(root, "lamp"))
    {
        int turnto_state = first_item->valueint;
        int led_id = second_item->valueint;
        printf("Turn to %d, LED ID: %d\n", turnto_state, led_id);
        switch (led_id)
        {
        case LED1_ID:
            if (turnto_state)
            {
                led_on(LED1);
            }
            else if (!turnto_state)
            {
                led_off(LED1);
            }
            break;
        case LED2_ID:
            if (turnto_state)
            {
                led_on(LED2);
            }
            else if (!turnto_state)
            {
                led_off(LED2);
            }
            break;
        case LED3_ID:
            if (turnto_state)
            {
                led_on(LED3);
            }
            else if (!turnto_state)
            {
                led_off(LED3);
            }
        default:
            printf("Error: Invalid LED ID\n");
        }
    }
    else if (cJSON_GetObjectItem(root, "fan"))
    {
        if (first_item->valueint == 1)
        {
            fan_on();
        }
        else if (first_item->valueint == 0)
        {
            fan_off();
        }
    }
    else if (cJSON_GetObjectItem(root, "alarm"))
    {
        if (first_item->valueint == 1)
        {
            buz_on();
        }
        else if (first_item->valueint == 0)
        {
            buz_off();
        }
    }
}
struct mosquitto *mos_init()
{
    int mqtt_init = mosquitto_lib_init();
    if (mqtt_init != MOSQ_ERR_SUCCESS)

    {
        printf("init error\n");
        return NULL;
    }

    struct mosquitto *mqtt_mosquitto = mosquitto_new(NULL, true, NULL);
    if (mqtt_mosquitto == NULL)
    {
        printf("mosquitto error\n");
        mosquitto_lib_cleanup();
        return NULL;
    }
    // 注册消息接受回调函数
    mosquitto_message_callback_set(mqtt_mosquitto, on_message);

    int mqtt_connect = mosquitto_connect(mqtt_mosquitto, MOS_IP, MOS_PORT, 10);
    if (mqtt_connect != MOSQ_ERR_SUCCESS)
    {
        printf("init error\n");
        mosquitto_destroy(mqtt_mosquitto);
        mosquitto_lib_cleanup();
        return NULL;
    }
    return mqtt_mosquitto;
}

void mos_destroy(struct mosquitto *mqtt_mosquitto)
{
    mosquitto_destroy(mqtt_mosquitto);
    mosquitto_lib_cleanup();
}

int mos_send(struct mosquitto *mqtt_mosquitto, int payloadlen, const void *payload)
{
    int mqtt_publish_res = mosquitto_publish(mqtt_mosquitto, NULL, MOS_TOPIC_CENT, payloadlen, payload, MOS_QOS_0, false);
    if (mqtt_publish_res != MOSQ_ERR_SUCCESS)
    {
        printf("%s\n", mosquitto_strerror(mqtt_publish_res));
        mosquitto_destroy(mqtt_mosquitto);
        mosquitto_lib_cleanup();
        return -1;
    }
    return 0;
}

int mos_subscribe(struct mosquitto *mqtt_mosquitto)
{

    int mqtt_subscribe_res = mosquitto_subscribe(mqtt_mosquitto, NULL, MOS_TOPIC_RECV, MOS_QOS_0);
    if (mqtt_subscribe_res != MOSQ_ERR_SUCCESS)
    {
        printf("%s\n", mosquitto_strerror(mqtt_subscribe_res));
        mosquitto_destroy(mqtt_mosquitto);
        mosquitto_lib_cleanup();
        return -1;
    }
    mosquitto_loop_start(mqtt_mosquitto);
    return 0;
}